
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include( 'shared.lua' )

function ENT:Initialize()
	
	self:SetModel("models/props_c17/trappropeller_lever.mdl")
	self:SetMaterial("models/shiny")
	self:SetColor(0,255,0,170)
	self.OmNom = Sound("weapons/physcannon/physcannon_tooheavy.wav")
	self.Dead = false
	self.ActivateTime = CurTime() + math.Rand(1,1.5)
	self.TeleTime = CurTime() + 10
	
	self.Entity:PhysicsInitSphere( 10, "gmod_bouncy" )
	self.Entity:SetCollisionBounds( Vector( -10, -10, -10 ), Vector( 10, 10, 10 ) )
	
	self:StartMotionController()
	self:SetCollisionGroup(COLLISION_GROUP_DEBRIS)
	
	local phys = self:GetPhysicsObject()
	if phys:IsValid() then
		phys:Wake()
		phys:ApplyForceCenter(VectorRand() * 50)
	end
	
end

function ENT:SetTarget(ply)
	self.Target = ply
end

function ENT:Think()
	if self.Target then
		if not self.Target:IsValid() then
			self:Remove()
		end
		if self.TeleTime < CurTime() then
			self.TeleTime = CurTime() + 10
			if self.Target:Team() != TEAM_HUMANS then
				self.Dead = true
			else
				self:SetPos(self.Target:GetPos() + self.Target:GetForward() * -55)
			end
		end
	end
	if self.Dead then
		if self.Target then
			if self.Target:IsValid() then
				if self.Target:Team() != TEAM_UNDEAD then
					self.Target:AddPoints(1)
					self.Target:EmitSound(self.OmNom,60,math.random(150,160))
					local effectdata = EffectData()
					effectdata:SetOrigin(self:GetPos())
					util.Effect("plutonium_take", effectdata,true,true)
				end
			end
		end
		self:Remove()
	end
end

function ENT:OnTakeDamage(dmg)
end 

function ENT:Use(ply,caller)
	self.Dead = true
end

function ENT:PhysicsSimulate(phys, frametime)
	phys:Wake()
	if self.Dead then return SIM_NOTHING end
	if self.Target then 
		if self.Target:IsValid() and self.Target != NULL then
			local dist = self.Target:GetPos():Distance(self:GetPos())
			if dist < 700 then
				if dist < 50 then
					self.Dead = true 
					return SIM_NOTHING
				elseif self.ActivateTime < CurTime() then
					local dir = (self:GetPos() - self.Target:GetPos()):Normalize()
					phys:ApplyForceCenter((dir * (math.Clamp(dist / 25,20,1000) * -1)))
				end
			end
		end
	end
end

function ENT:PhysicsCollide( data, phys )
	if data.Speed > 60 && data.DeltaTime > 0.2 then
		self:EmitSound(ChooseRandom(GameSounds.PlutHit),60,math.random(130,150))
	end
end
